import logging

import requests
import yaml

from common import BeautySoupTool
from common import DateTool

session = requests.session()
logger = logging.getLogger(__name__)


def do_login():
    login_url = 'http://exam.chinapmp.cn/App_Ajax/SHOW.Ajax.Exam.Login,SHOW.Ajax.ajax?id =3&doing=view&domain=exam'
    JSON_DATA = [
        {
            "Name": "uType",
            "Value": 0
        },
        {
            "Name": "uName",
            "Value": cn_loginName
        },
        {
            "Name": "uPass",
            "Value": cn_pwd
        }
    ]
    login_res = session.post(login_url, {'JSON_DATA': JSON_DATA})
    return login_res


# 访问报名页面
def do_register():
    # 登录成功使用以下报名
    register_url = 'http://user.chinapmp.cn/examsign;sign.shtml'
    return session.get(register_url, timeout=(20, 10))


# 保存报名数据
def post_data():
    register_data = [
        {
            "Name": "Ed",
            "Value": 10000044
        },
        {
            "Name": "Etitle",
            "Value": " 2021年6月20日项目管理资格认证考试"
        },
        {
            "Name": "Stype",
            "Value": 101
        },
        {
            "Name": "StypeName",
            "Value": "项目管理师(PMP®)"
        },
        {
            "Name": "Xing",
            "Value": FIRST_NAME
        },
        {
            "Name": "Zhong",
            "Value": ""
        },
        {
            "Name": "Ming",
            "Value": LAST_NAME
        },
        {
            "Name": "Peixunjigou",
            "Value": "515"
        },
        {
            "Name": "Peixunjigouming",
            "Value": "北京光环致成国际管理咨询股份有限公司"
        },
        {
            "Name": "PMIUname",
            "Value": USER_NAME
        },
        {
            "Name": "PMIUpass",
            "Value": PASSWORD
        },
        {
            "Name": "IsPMIUser",
            "Value": 'false'
        },
        {
            "Name": "PMINumber",
            "Value": ""
        },
        {
            "Name": "_PMIUtimeB",
            "Value": ""
        },
        {
            "Name": "_PMIUtimeE",
            "Value": ""
        },
        {
            "Name": "PMItimeB",
            "Value": "/Date(" + DateTool.date_to_timestamp(START_TIME) + "+0800)/"
        },
        {
            "Name": "PMItimeE",
            "Value": "/Date(" + DateTool.date_to_timestamp(END_TIME) + "+0800)/"
        },
        {
            "Name": "Kaodian",
            "Value": kao_dian
        },
        {
            "Name": "Kaodianming",
            "Value": kao_dian_ming
        },
        {
            "Name": "PMIID",
            "Value": PMI_ID
        }
    ]
    url = ''
    session_post = session.post(url, {'JSON_DATA': register_data})
    return session_post


if __name__ == '__main__':
    # config = DoConfig.DoConfig('config.ini')
    with open('config.yml', encoding='utf8') as c:
        config = yaml.load(c, Loader=yaml.FullLoader)
    pmi = config['PMI']
    cn_pmi = config['CN-PMI']
    kao_dian_dict = config['kao_dian_dict']
    kao_dian = int(input('请输入考点编号，参考config.yml配置文件中kao_dian_dict项！！！'))
    # print(type(kao_dian))
    kao_dian_ming = kao_dian_dict[kao_dian]
    PMI_ID = pmi['PMI_ID']
    START_TIME = pmi['START_TIME']
    END_TIME = pmi['END_TIME']
    USER_NAME = pmi['USER_NAME']
    PASSWORD = pmi['PASSWORD']

    FIRST_NAME = cn_pmi['FIRST_NAME']
    LAST_NAME = cn_pmi['LAST_NAME']
    cn_loginName = cn_pmi['LOGIN_NAME']
    cn_pwd = cn_pmi['PASSWORD']
    logger.info('开始访问首页。。。。。。。%s', 'http://exam.chinapmp.cn/')
    tool = BeautySoupTool.BeautySoupTool("http://exam.chinapmp.cn/")
    while tool.error is True or tool.status_code != 200:
        logger.info(tool.status_code)
        logger.info('访问失败，重新访问。。。')
        tool = BeautySoupTool.BeautySoupTool("http://exam.chinapmp.cn/")
    # 开始登录
    logger.info('访问成功，开始登录。。。。。。。%s',
                'http://exam.chinapmp.cn/App_Ajax/SHOW.Ajax.Exam.Login,SHOW.Ajax.ajax?id =3&doing=view&domain=exam')
    login = do_login()
    while login.status_code != 200:
        logger.error('---登录失败，尝试重新登录......')
        login = do_login()
    logger.info('登录成功，访问报名页面中。。。。')
    register = do_register()
    while register.status_code != 200:
        logger.info('访问报名页面失败，重新访问中。。。。')
        register = do_register()
    logger.info('访问报名页面成功，开始报名。。。')
    response = post_data()
    while response.status_code != 200:
        logger.info('报名失败，重新报名中。。。。')
        response = post_data()
